import { MES_XSP_ApiHost } from '@/utils/Interceptors'

// 物料清单详情DTO
export interface BillOfMaterialDetailDTO {
    billOfMaterialId: number
    parentProductId: number
    parentProductCode: string
    parentProductName: string
    childProductId: number
    childProductCode: string
    childProductName: string
    unitUsage: number
    remark?: string
    createTime: string
    creator: string
    modifyTime?: string
    modifier?: string
}

// 物料清单创建DTO
export interface BillOfMaterialCreateDTO {
    parentProductId: number
    childProductId: number
    unitUsage: number
    remark?: string
    creator: string
}

// 物料清单更新DTO
export interface BillOfMaterialUpdateDTO {
    billOfMaterialId: number
    parentProductId: number
    childProductId: number
    unitUsage: number
    remark?: string
    modifier: string
}

// 物料清单查询参数
export interface BillOfMaterialQueryParams {
    pageIndex: number
    pageSize: number
}

// 产品接口
export interface Product {
    productId: number
    productCode: string
    productName: string
    productSpec?: string
    inventoryUnit: string
    productAttribute: string
    currentInventory: number
    maxInventory?: number
    minInventory?: number
    safetyInventory?: number
    productImage?: string
    createTime: string
    creator: string
    modifyTime?: string
    modifier?: string
    isDelete: boolean
}

// API响应接口
export interface ApiResult<T> {
    code: number
    msg: string
    data: T
}

export interface ApiPaging<T> {
    code: number
    msg: string
    pageData: T[]
    totalCount: number
    totalPage: number
}

// 获取物料清单列表（分页）
export const getBillOfMaterialList = async (params: BillOfMaterialQueryParams): Promise<{ data: ApiPaging<BillOfMaterialDetailDTO> }> => {
    return await MES_XSP_ApiHost.get('/api/BillOfMaterialGetBillOfMaterialList', { params })
}

// 根据ID获取物料清单详情
export const getBillOfMaterialById = async (billOfMaterialId: number): Promise<{ data: ApiResult<BillOfMaterialDetailDTO> }> => {
    return await MES_XSP_ApiHost.get(`/api/BillOfMaterialGetBillOfMaterialById`, {
        params: { billOfMaterialId }
    })
}

// 创建物料清单
export const createBillOfMaterial = async (data: BillOfMaterialCreateDTO): Promise<{ data: ApiResult<boolean> }> => {
    return await MES_XSP_ApiHost.post('/api/BillOfMaterialCreateBillOfMaterial', data)
}

// 更新物料清单
export const updateBillOfMaterial = async (data: BillOfMaterialUpdateDTO): Promise<{ data: ApiResult<boolean> }> => {
    return await MES_XSP_ApiHost.put('/api/BillOfMaterialUpdateBillOfMaterial', data)
}

// 删除物料清单
export const deleteBillOfMaterial = async (billOfMaterialId: number, modifier: string): Promise<{ data: ApiResult<boolean> }> => {
    return await MES_XSP_ApiHost.delete(`/api/BillOfMaterial/DeleteBillOfMaterial`, {
        params: { billOfMaterialId, modifier }
    })
}

// 批量删除物料清单
export const batchDeleteBillOfMaterial = async (billOfMaterialIds: number[], modifier: string): Promise<{ data: ApiResult<boolean> }> => {
    return await MES_XSP_ApiHost.delete('/api/BillOfMaterial/BatchDeleteBillOfMaterial', {
        data: billOfMaterialIds,
        params: { modifier }
    })
}

// 检查物料清单是否存在
export const checkBillOfMaterialExists = async (parentProductId: number, childProductId: number, excludeId?: number): Promise<{ data: ApiResult<boolean> }> => {
    const params: any = { parentProductId, childProductId }
    if (excludeId !== undefined) {
        params.excludeId = excludeId
    }
    return await MES_XSP_ApiHost.get('/api/BillOfMaterial/CheckBillOfMaterialExists', { params })
}

// 获取所有产品选项
export const getAllProductOptions = async (): Promise<{ data: ApiResult<Product[]> }> => {
    return await MES_XSP_ApiHost.get('/api/BillOfMaterialGetAllProductOptions')
} 